package com.xiaomili.halfsugar.fragment;


import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.SearchHomepageVP_Adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页面----单品、清单的碎片
 * A simple {@link Fragment} subclass.
 */
public class SearchHomePageFragment extends Fragment {
    private List<Fragment> mList;
    private int mCurrentIndex, mPreviousIndex;
    private RadioGroup mRadioGroup;
    private ViewPager vp;
    private ImageView iv;

    public SearchHomePageFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search_home_page, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initFragment();
        vp = (ViewPager) view.findViewById(R.id.search_homepage_vp);
        mRadioGroup = (RadioGroup) view.findViewById(R.id.search_homepage_rg);
        iv = (ImageView) view.findViewById(R.id.search_homepage_iv);
        SearchHomepageVP_Adapter adapter = new SearchHomepageVP_Adapter(getFragmentManager(), mList);
        adapter.notifyDataSetChanged();
        vp.setAdapter(adapter);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                mCurrentIndex = group.indexOfChild(rb);
                vp.setCurrentItem(mCurrentIndex);
                initAnimation();
                mPreviousIndex = mCurrentIndex;
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) mRadioGroup.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    //碎片添加
    private void initFragment() {
        mList = new ArrayList<>();
        mList.add(new SearchProductFragment());
        mList.add(new SearchManifestFragment());
    }

    private void initAnimation() {
        //构造函数DisplayMetrics 不需要传递任何参数；调用getWindowManager() 之后，会取得现有Activity 的Handle ，此时，
        // getDefaultDisplay() 方法将取得的宽高维度存放于DisplayMetrics 对象中，而取得的宽高维度是以像素为单位(Pixel) ，
        // “像素”所指的是 “绝对像素”而非“相对像素”。
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int widthPixels = displayMetrics.widthPixels / 2;
        int One = 0, Two = One + widthPixels;
        switch (mCurrentIndex) {
            case 0:
                if (mPreviousIndex == 1) {
                    ObjectAnimator.ofFloat(iv, "translationX", Two, One).setDuration(300).start();
                }
                break;
            case 1:
                if (mPreviousIndex == 0) {
                    ObjectAnimator.ofFloat(iv, "translationX", One, Two).setDuration(300).start();
                }
                break;
        }
    }
}
